home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / mint / netlib / lib / rsend.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-27  |  11.5 KB  |  452 lines

  1. /*
  2.  * Adopted to Mint-Net 1994, Kay Roemer
  3.  */
  4.  
  5. /*
  6.  * Copyright (c) 1985, 1989 Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  *    This product includes software developed by the University of
  20.  *    California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  */
  37.  
  38. #if defined(LIBC_SCCS) && !defined(lint)
  39. static char sccsid[] = "@(#)res_send.c    6.27 (Berkeley) 2/24/91";
  40. #endif /* LIBC_SCCS and not lint */
  41.  
  42. /*
  43.  * Send query to name server and wait for reply.
  44.  */
  45.  
  46. #include "socklib.h"
  47. #include <sys/types.h>
  48. #include <sys/param.h>
  49. #include <sys/time.h>
  50. #include <sys/socket.h>
  51. #if 0
  52. #include <sys/uio.h>
  53. #endif
  54. #include <netinet/in.h>
  55. #include <arpa/nameser.h>
  56. #include <stdio.h>
  57. #include <errno.h>
  58. #include <resolv.h>
  59. #include <unistd.h>
  60. #include <string.h>
  61.  
  62. static int s = -1;    /* socket used for communications */
  63. static struct sockaddr no_addr;
  64.  
  65. #ifndef FD_SET
  66. #define    NFDBITS        32
  67. #define    FD_SETSIZE    32
  68. #define    FD_SET(n, p)    ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
  69. #define    FD_CLR(n, p)    ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
  70. #define    FD_ISSET(n, p)    ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
  71. #define FD_ZERO(p)    bzero((char *)(p), sizeof(*(p)))
  72. #endif
  73.  
  74. int
  75. res_send(buf, buflen, answer, anslen)
  76.     const char *buf;
  77.     int buflen;
  78.     char *answer;
  79.     int anslen;
  80. {
  81.     register int n;
  82.     int try, v_circuit, resplen, ns;
  83.     int gotsomewhere = 0, connected = 0;
  84.     int connreset = 0;
  85.     u_short id, len;
  86.     char *cp;
  87.     fd_set dsmask;
  88.     struct timeval timeout;
  89.     HEADER *hp = (HEADER *) buf;
  90.     HEADER *anhp = (HEADER *) answer;
  91.     struct iovec iov[2];
  92.     int terrno = ETIMEDOUT;
  93.     char junk[512];
  94.  
  95. #ifdef DEBUG
  96.     if (_res.options & RES_DEBUG) {
  97.         printf("res_send()\n");
  98.         __p_query(buf);
  99.     }
  100. #endif DEBUG
  101.     if (!(_res.options & RES_INIT))
  102.         if (res_init() == -1) {
  103.             return(-1);
  104.         }
  105.     v_circuit = (_res.options & RES_USEVC) || buflen > PACKETSZ;
  106.     id = hp->id;
  107.     /*
  108.      * Send request, RETRY times, or until successful
  109.      */
  110.     for (try = 0; try < _res.retry; try++) {
  111.        for (ns = 0; ns < _res.nscount; ns++) {
  112. #ifdef DEBUG
  113.         if (_res.options & RES_DEBUG)
  114.             printf("Querying server (# %d) address = %s\n", ns+1,
  115.                   inet_ntoa(_res.nsaddr_list[ns].sin_addr));
  116. #endif DEBUG
  117.     usevc:
  118.         if (v_circuit) {
  119.             int truncated = 0;
  120.  
  121.             /*
  122.              * Use virtual circuit;
  123.              * at most one attempt per server.
  124.              */
  125.             try = _res.retry;
  126.             if (s < 0) {
  127.                 s = socket(AF_INET, SOCK_STREAM, 0);
  128.                 if (s < 0) {
  129.                     terrno = errno;
  130. #ifdef DEBUG
  131.                     if (_res.options & RES_DEBUG)
  132.                         perror("socket (vc) failed");
  133. #endif DEBUG
  134.                     continue;
  135.                 }
  136.                 if (connect(s,
  137.                     (struct sockaddr *)&(_res.nsaddr_list[ns]),
  138.                     sizeof(struct sockaddr)) < 0) {
  139.                     terrno = errno;
  140. #ifdef DEBUG
  141.                     if (_res.options & RES_DEBUG)
  142.                         perror("connect failed");
  143. #endif DEBUG
  144.                     (void) close(s);
  145.                     s = -1;
  146.                     continue;
  147.                 }
  148.             }
  149.             /*
  150.              * Send length & message
  151.              */
  152.             len = htons((u_short)buflen);
  153.             iov[0].iov_base = (caddr_t)&len;
  154.             iov[0].iov_len = sizeof(len);
  155.             iov[1].iov_base = (char *)buf;
  156.             iov[1].iov_len = buflen;
  157.             {
  158.                 struct msghdr msg;
  159.                 msg.msg_name = 0;
  160.                 msg.msg_namelen = 0;
  161.                 msg.msg_iov = iov;
  162.                 msg.msg_iovlen = 2;
  163.                 msg.msg_accrights = 0;
  164.                 msg.msg_accrightslen = 0;
  165.                 
  166.                 if (sendmsg(s, &msg, 0) != sizeof(len) + buflen) {
  167.                     terrno = errno;
  168. #ifdef DEBUG
  169.                     if (_res.options & RES_DEBUG)
  170.                         perror("write failed");
  171. #endif DEBUG
  172.                     (void) close(s);
  173.                     s = -1;
  174.                     continue;
  175.                 }
  176.             }
  177.             /*
  178.              * Receive length & response
  179.              */
  180.             cp = answer;
  181.             len = sizeof(short);
  182.             while (len != 0 &&
  183.                 (n = read(s, (char *)cp, (int)len)) > 0) {
  184.                 cp += n;
  185.                 len -= n;
  186.             }
  187.             if (n <= 0) {
  188.                 terrno = errno;
  189. #ifdef DEBUG
  190.                 if (_res.options & RES_DEBUG)
  191.                     perror("read failed");
  192. #endif DEBUG
  193.                 (void) close(s);
  194.                 s = -1;
  195.                 /*
  196.                  * A long running process might get its TCP
  197.                  * connection reset if the remote server was
  198.                  * restarted.  Requery the server instead of
  199.                  * trying a new one.  When there is only one
  200.                  * server, this means that a query might work
  201.                  * instead of failing.  We only allow one reset
  202.                  * per query to prevent looping.
  203.                  */
  204.                 if (terrno == ECONNRESET && !connreset) {
  205.                     connreset = 1;
  206.                     ns--;
  207.                 }
  208.                 continue;
  209.             }
  210.             cp = answer;
  211.             if ((resplen = ntohs(*(u_short *)cp)) > anslen) {
  212. #ifdef DEBUG
  213.                 if (_res.options & RES_DEBUG)
  214.                     fprintf(stderr, "response truncated\n");
  215. #endif DEBUG
  216.                 len = anslen;
  217.                 truncated = 1;
  218.             } else
  219.                 len = resplen;
  220.             while (len != 0 &&
  221.                (n = read(s, (char *)cp, (int)len)) > 0) {
  222.                 cp += n;
  223.                 len -= n;
  224.             }
  225.             if (n <= 0) {
  226.                 terrno = errno;
  227. #ifdef DEBUG
  228.                 if (_res.options & RES_DEBUG)
  229.                     perror("read failed");
  230. #endif DEBUG
  231.                 (void) close(s);
  232.                 s = -1;
  233.                 continue;
  234.             }
  235.             if (truncated) {
  236.                 /*
  237.                  * Flush rest of answer
  238.                  * so connection stays in synch.
  239.                  */
  240.                 anhp->tc = 1;
  241.                 len = resplen - anslen;
  242.                 while (len != 0) {
  243.                     n = (len > sizeof(junk) ?
  244.                         sizeof(junk) : len);
  245.                     if ((n = read(s, junk, n)) > 0)
  246.                         len -= n;
  247.                     else
  248.                         break;
  249.                 }
  250.             }
  251.         } else {
  252.             /*
  253.              * Use datagrams.
  254.              */
  255.             if (s < 0) {
  256.                 s = socket(AF_INET, SOCK_DGRAM, 0);
  257.                 if (s < 0) {
  258.                     terrno = errno;
  259. #ifdef DEBUG
  260.                     if (_res.options & RES_DEBUG)
  261.                         perror("socket (dg) failed");
  262. #endif DEBUG
  263.                     continue;
  264.                 }
  265.             }
  266. #if (BSD >= 43) || defined (__MINT__)
  267.             /*
  268.              * I'm tired of answering this question, so:
  269.              * On a 4.3BSD+ machine (client and server,
  270.              * actually), sending to a nameserver datagram
  271.              * port with no nameserver will cause an
  272.              * ICMP port unreachable message to be returned.
  273.              * If our datagram socket is "connected" to the
  274.              * server, we get an ECONNREFUSED error on the next
  275.              * socket operation, and select returns if the
  276.              * error message is received.  We can thus detect
  277.              * the absence of a nameserver without timing out.
  278.              * If we have sent queries to at least two servers,
  279.              * however, we don't want to remain connected,
  280.              * as we wish to receive answers from the first
  281.              * server to respond.
  282.              */
  283.             if (_res.nscount == 1 || (try == 0 && ns == 0)) {
  284.                 /*
  285.                  * Don't use connect if we might
  286.                  * still receive a response
  287.                  * from another server.
  288.                  */
  289.                 if (connected == 0) {
  290.             if (connect(s, (struct sockaddr *)&_res.nsaddr_list[ns],
  291.                         sizeof(struct sockaddr)) < 0) {
  292. #ifdef DEBUG
  293.                         if (_res.options & RES_DEBUG)
  294.                             perror("connect");
  295. #endif DEBUG
  296.                         continue;
  297.                     }
  298.                     connected = 1;
  299.                 }
  300.                 if (send(s, buf, buflen, 0) != buflen) {
  301. #ifdef DEBUG
  302.                     if (_res.options & RES_DEBUG)
  303.                         perror("send");
  304. #endif DEBUG
  305.                     continue;
  306.                 }
  307.             } else {
  308.                 /*
  309.                  * Disconnect if we want to listen
  310.                  * for responses from more than one server.
  311.                  */
  312.                 if (connected) {
  313.                     (void) connect(s, &no_addr,
  314.                         sizeof(no_addr));
  315.                     connected = 0;
  316.                 }
  317. #endif BSD || __MINT__
  318.                 if (sendto(s, buf, buflen, 0,
  319.                     (struct sockaddr *)&_res.nsaddr_list[ns],
  320.                     sizeof(struct sockaddr)) != buflen) {
  321. #ifdef DEBUG
  322.                     if (_res.options & RES_DEBUG)
  323.                         perror("sendto");
  324. #endif DEBUG
  325.                     continue;
  326.                 }
  327. #if (BSD >= 43) || defined (__MINT__)
  328.             }
  329. #endif
  330.  
  331.             /*
  332.              * Wait for reply
  333.              */
  334.             timeout.tv_sec = (_res.retrans << try);
  335.             if (try > 0)
  336.                 timeout.tv_sec /= _res.nscount;
  337.             if (timeout.tv_sec <= 0)
  338.                 timeout.tv_sec = 1;
  339.             timeout.tv_usec = 0;
  340. wait:
  341.             FD_ZERO(&dsmask);
  342.             FD_SET(s, &dsmask);
  343.             n = select(s+1, &dsmask, (fd_set *)NULL,
  344.                 (fd_set *)NULL, &timeout);
  345.             if (n < 0) {
  346. #ifdef DEBUG
  347.                 if (_res.options & RES_DEBUG)
  348.                     perror("select");
  349. #endif DEBUG
  350.                 continue;
  351.             }
  352.             if (n == 0) {
  353.                 /*
  354.                  * timeout
  355.                  */
  356. #ifdef DEBUG
  357.                 if (_res.options & RES_DEBUG)
  358.                     printf("timeout\n");
  359. #endif DEBUG
  360. #if (BSD >= 43) || defined (__MINT__)
  361.                 gotsomewhere = 1;
  362. #endif
  363.                 continue;
  364.             }
  365.             if ((resplen = recv(s, answer, anslen, 0)) <= 0) {
  366. #ifdef DEBUG
  367.                 if (_res.options & RES_DEBUG)
  368.                     perror("recvfrom");
  369. #endif DEBUG
  370.                 continue;
  371.             }
  372.             gotsomewhere = 1;
  373.             if (id != anhp->id) {
  374.                 /*
  375.                  * response from old query, ignore it
  376.                  */
  377. #ifdef DEBUG
  378.                 if (_res.options & RES_DEBUG) {
  379.                     printf("old answer:\n");
  380.                     __p_query(answer);
  381.                 }
  382. #endif DEBUG
  383.                 goto wait;
  384.             }
  385.             if (!(_res.options & RES_IGNTC) && anhp->tc) {
  386.                 /*
  387.                  * get rest of answer;
  388.                  * use TCP with same server.
  389.                  */
  390. #ifdef DEBUG
  391.                 if (_res.options & RES_DEBUG)
  392.                     printf("truncated answer\n");
  393. #endif DEBUG
  394.                 (void) close(s);
  395.                 s = -1;
  396.                 v_circuit = 1;
  397.                 goto usevc;
  398.             }
  399.         }
  400. #ifdef DEBUG
  401.         if (_res.options & RES_DEBUG) {
  402.             printf("got answer:\n");
  403.             __p_query(answer);
  404.         }
  405. #endif DEBUG
  406.         /*
  407.          * If using virtual circuits, we assume that the first server
  408.          * is preferred * over the rest (i.e. it is on the local
  409.          * machine) and only keep that one open.
  410.          * If we have temporarily opened a virtual circuit,
  411.          * or if we haven't been asked to keep a socket open,
  412.          * close the socket.
  413.          */
  414.         if ((v_circuit &&
  415.             ((_res.options & RES_USEVC) == 0 || ns != 0)) ||
  416.             (_res.options & RES_STAYOPEN) == 0) {
  417.             (void) close(s);
  418.             s = -1;
  419.         }
  420.         return (resplen);
  421.        }
  422.     }
  423.     if (s >= 0) {
  424.         (void) close(s);
  425.         s = -1;
  426.     }
  427.     if (v_circuit == 0)
  428.         if (gotsomewhere == 0)
  429.             errno = ECONNREFUSED;    /* no nameservers found */
  430.         else
  431.             errno = ETIMEDOUT;    /* no answer obtained */
  432.     else
  433.         errno = terrno;
  434.     return (-1);
  435. }
  436.  
  437. /*
  438.  * This routine is for closing the socket if a virtual circuit is used and
  439.  * the program wants to close it.  This provides support for endhostent()
  440.  * which expects to close the socket.
  441.  *
  442.  * This routine is not expected to be user visible.
  443.  */
  444. void
  445. _res_close()
  446. {
  447.     if (s != -1) {
  448.         (void) close(s);
  449.         s = -1;
  450.     }
  451. }
  452.